package com.aiti.lulian.utils;

import com.aiti.lulian.entity.LuLianDockingMeetingPurchaserPurchaseItem;
import com.aiti.lulian.entity.LuLianDockingMeetingPurchaserSignupInformation;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

public class ActivityCaiGouExcel {

    public static void  getRow1( HSSFSheet sheet,HSSFWorkbook wb){
        HSSFRow row1=sheet.createRow(0);
        row1.setHeight((short)(20*30));
        int width1 = (int) ((40 + 0.72) * 256);
        sheet.setColumnWidth(0,width1);
        HSSFCell cell = row1.createCell( 0);
        //设置单元格内容
        ActivityCaiGouExcel.setSize(wb,cell,0);
        cell.setCellValue("采购商报名数据");
        //合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,10));
    }

//    public static void  setSize(HSSFWorkbook wb, HSSFCell cell, int i){
//        HSSFFont font = wb.createFont();
//        font.setbold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//        HSSFCellStyle cellStyle= wb.createCellStyle();
//        cellStyle.setFont(font);
//        cell.setCellStyle(cellStyle);
//        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//
//        if(i==0){
//            cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());// 设置背景色
//            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//            font.setFontHeightInPoints((short) 16);//设置字体大小
//        }else{
//            font.setFontHeightInPoints((short) 12);//设置字体大小
//        }
//
//    }

    public static void setSize(HSSFWorkbook wb, HSSFCell cell, int i) {
        // 创建字体
        Font font = wb.createFont();
        font.setBold(true);  // 设置粗体
        if (i == 0) {
            font.setFontHeightInPoints((short) 16);  // 设置字体大小为16
        } else {
            font.setFontHeightInPoints((short) 12);  // 设置字体大小为12
        }

        // 创建单元格样式
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);  // 设置水平居中对齐

        // 设置背景色和填充模式（仅当 i == 0 时）
        if (i == 0) {
            cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        }

        // 设置单元格样式
        cell.setCellStyle(cellStyle);

        // 设置单元格类型为字符串
        cell.setCellType(CellType.STRING);
    }


    public static void setWidth(HSSFRow row2, HSSFSheet sheet){
        for (int j = 0; j <=15; j++) {
            if(j<=1){
                row2.setHeight((short)(20*30));
                int width = (int) ((40 + 0.72) * 256);
                sheet.setColumnWidth(j,width);
            }else if(j==5){
                row2.setHeight((short)(20*30));
                int width = (int) ((50 + 0.72) * 256);
                sheet.setColumnWidth(j,width);
            }else if(5<j){
                row2.setHeight((short)(20*30));
                int width = (int) ((20 + 0.72) * 256);
                sheet.setColumnWidth(j,width);
            }
        }
    }

    public static void getRow2(HSSFRow row2, HSSFWorkbook wb){
        HSSFCell cell1 =   row2.createCell(0);
        cell1.setCellValue("企业名称");
        ActivityCaiGouExcel.setSize(wb,cell1,1);
        HSSFCell cell2 =  row2.createCell(1);
        cell2.setCellValue("一级行业");
        ActivityCaiGouExcel.setSize(wb,cell2,1);
        HSSFCell cell3=row2.createCell(2);
        cell3.setCellValue("二级行业");
        ActivityCaiGouExcel.setSize(wb,cell3,1);
        HSSFCell cell4=row2.createCell(3);
        cell4.setCellValue("省");
        ActivityCaiGouExcel.setSize(wb,cell4,1);
        HSSFCell cell5=row2.createCell(4);
        cell5.setCellValue("市");
        ActivityCaiGouExcel.setSize(wb,cell5,1);
        HSSFCell cell6=row2.createCell(5);
        cell6.setCellValue("区/县");
        ActivityCaiGouExcel.setSize(wb,cell6,1);
        HSSFCell cell7=row2.createCell(6);
        cell7.setCellValue("具体地址");
        ActivityCaiGouExcel.setSize(wb,cell7,1);
        HSSFCell cell8=row2.createCell(7);
        cell8.setCellValue("联系人姓名");
        ActivityCaiGouExcel.setSize(wb,cell8,1);
        HSSFCell cell9=row2.createCell(8);
        cell9.setCellValue("职位");
        ActivityCaiGouExcel.setSize(wb,cell9,1);
        row2.createCell(9);
        HSSFCell cell10=  row2.createCell(9);
        cell10.setCellValue("手机号");
        ActivityCaiGouExcel.setSize(wb,cell10,1);
    }

    public static void excelList(HttpServletResponse response, List<LuLianDockingMeetingPurchaserSignupInformation> list){
        try{
            //创建HSSFWorkbook对象
            HSSFWorkbook wb = new HSSFWorkbook();
            //建立sheet对象
            HSSFSheet sheet=wb.createSheet("报名信息");
            ActivityCaiGouExcel.getRow1(sheet,wb);

            //在sheet里创建第二行
            HSSFRow row2=sheet.createRow(1);
            ActivityCaiGouExcel.setWidth(row2,sheet);

            //创建单元格并设置单元格内容
            ActivityCaiGouExcel.getRow2(row2,wb);

            for (int i = 0; i <list.size() ; i++) {
                HSSFRow row3=sheet.createRow(i+2);
                row3.createCell(0).setCellValue(list.get(i).getEnterpriseName());
                row3.createCell(1).setCellValue(list.get(i).getIndustryDictFirst());
                row3.createCell(2).setCellValue(list.get(i).getIndustryDictSecond());
                row3.createCell(3).setCellValue(list.get(i).getProvince());
                row3.createCell(4).setCellValue(list.get(i).getCity());
                row3.createCell(5).setCellValue(list.get(i).getCounty());
                row3.createCell(6).setCellValue(list.get(i).getAddress());
                row3.createCell(7).setCellValue(list.get(i).getPrincipalName());
                row3.createCell(8).setCellValue(list.get(i).getPosition());
                row3.createCell(9).setCellValue(list.get(i).getPrincipalTel());
            }
            //输出Excel文件
            OutputStream output=response.getOutputStream();
            response.reset();
            //设置响应头
            String fileName="采购商报名数据.xlsx";
            response.setHeader("Content-disposition", "attachment; filename="+ ActivityCaiGouExcel.toUtf8String(fileName));
            response.setContentType("application/msexcel");
            wb.write(output);
            output.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static String toUtf8String(String s){
        StringBuffer sb = new StringBuffer();
        for (int i=0;i<s.length();i++){
            char c = s.charAt(i);
            if (c >= 0 && c <= 255){sb.append(c);}
            else{
                byte[] b;
                try { b = Character.toString(c).getBytes("utf-8");}
                catch (Exception ex) {
                    System.out.println(ex);
                    b = new byte[0];
                }
                for (int j = 0; j < b.length; j++) {
                    int k = b[j];
                    if (k < 0) k += 256;
                    sb.append("%" + Integer.toHexString(k).toUpperCase());
                }
            }
        }
        return sb.toString();
    }

}
